use tokio;
use std::sync::Arc;
use std::time::Duration;
use vertica_rs::{
    pool::{ConnectionPool, PoolConfig},
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create connection configuration
    // Configure connection pool
    let pool_config = PoolConfig {
        max_connections: 10,
        connection_timeout: Duration::from_secs(30),
    };

    let pool: Arc<ConnectionPool> = ConnectionPool::new(pool_config);

    // Simulate multiple concurrent connections
    let mut handles = vec![];

    for i in 0..5 {
        let pool_clone = Arc::clone(&pool);
        
        let handle = tokio::spawn(async move {
            match pool_clone.get_connection() {
                Ok(mut client) => {
                    println!("Task {}: Got connection", i);
                    
                    // Execute a simple query
                    match client.simple_query("SELECT CURRENT_TIMESTAMP") {
                        Ok(result) => {
                            println!("Task {}: Query successful - {} rows", i, result.len());
                        }
                        Err(e) => {
                            eprintln!("Task {}: Query failed: {}", i, e);
                        }
                    }
                    
                    // Return connection to pool
                    client.close().unwrap();
                }
                Err(e) => {
                    eprintln!("Task {}: Failed to get connection: {}", i, e);
                }
            }
        });
        
        handles.push(handle);
    }

    // Wait for all tasks to complete
    for handle in handles {
        handle.await.unwrap();
    }

    println!("All threads completed!");

    Ok(())
}
